
package me.zhengjie.service.impl;

import me.zhengjie.domain.AppDocFav;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.AppDocFavRepository;
import me.zhengjie.service.AppDocFavService;
import me.zhengjie.service.dto.AppDocFavDto;
import me.zhengjie.service.dto.AppDocFavQueryCriteria;
import me.zhengjie.service.mapstruct.AppDocFavMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author IQW
* @date 2022-07-18
**/
@Service
@RequiredArgsConstructor
public class AppDocFavServiceImpl implements AppDocFavService {

    private final AppDocFavRepository appDocFavRepository;
    private final AppDocFavMapper appDocFavMapper;

    @Override
    public Map<String,Object> queryAll(AppDocFavQueryCriteria criteria, Pageable pageable){
        Page<AppDocFav> page = appDocFavRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(appDocFavMapper::toDto));
    }

    @Override
    public List<AppDocFavDto> queryAll(AppDocFavQueryCriteria criteria){
        return appDocFavMapper.toDto(appDocFavRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public Boolean findIsFav(Integer userId,Integer docId) {

        AppDocFavQueryCriteria criteria = new AppDocFavQueryCriteria();
        criteria.setUserId(userId);
        criteria.setDocId(docId);
        List<AppDocFavDto> ret = queryAll(criteria);
        if(ret.size()>0){
            return true;
        }
        return  false;
    }

    @Override
    @Transactional
    public AppDocFavDto findById(Integer id) {
        AppDocFav appDocFav = appDocFavRepository.findById(id).orElseGet(AppDocFav::new);
        ValidationUtil.isNull(appDocFav.getId(),"AppDocFav","id",id);
        return appDocFavMapper.toDto(appDocFav);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppDocFavDto create(AppDocFav resources) {
        return appDocFavMapper.toDto(appDocFavRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AppDocFav resources) {
        AppDocFav appDocFav = appDocFavRepository.findById(resources.getId()).orElseGet(AppDocFav::new);
        ValidationUtil.isNull( appDocFav.getId(),"AppDocFav","id",resources.getId());
        appDocFav.copy(resources);
        appDocFavRepository.save(appDocFav);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            appDocFavRepository.deleteById(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOne(Integer userId,Integer docId){
        appDocFavRepository.deleteOne(userId,docId);
    }

    @Override
    public void download(List<AppDocFavDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AppDocFavDto appDocFav : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户ID", appDocFav.getUserId());
            map.put("文档ID", appDocFav.getDocId());
            map.put(" createTime",  appDocFav.getCreateTime());
            map.put(" updateTime",  appDocFav.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}